package com.bfxy.kafka.api.interceptor;

import org.apache.kafka.clients.consumer.ConsumerInterceptor;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
import org.apache.kafka.common.TopicPartition;

import java.util.Map;

public class CustomConsumerInterceptor implements ConsumerInterceptor<String, String> {

    /**
     * 消费者接收到消息处理之前的拦截器
     * @param consumerRecords
     * @return
     */
    @Override
    public ConsumerRecords<String, String> onConsume(ConsumerRecords<String, String> consumerRecords) {
        System.out.println("--------------- 消费者前置拦截器，接收消息 ----------------");
        return consumerRecords;
    }

    /**
     * 消费者接收到消息处理完成后的拦截器
     * 如果配置了自动提交AUTO_COMMIT_INTERVAL_MS_CONFIG，没有消息处理完成也会每隔对应的时间执行一次该拦截器
     * 手动提交不会则只会在接收到消息处理完成后才会执行该拦截器
     * @param map
     */
    @Override
    public void onCommit(Map<TopicPartition, OffsetAndMetadata> map) {
        map.forEach((tp, offset) -> {
            System.out.println("消费者处理完成，" + "分区：" + tp + "，偏移量：" + offset);
        });
    }

    @Override
    public void close() {

    }

    @Override
    public void configure(Map<String, ?> map) {

    }

}

